<template>
    <div class="login-container">
        <div class="login-box">
            <div class="avatar-box">
                <img src="../assets/logo.png" style="height: 100px; width: 100px; border-radius: 100%;">
            </div>
            <div class="form-login">
                <div class="form-group">
                    <label for="username">用户名：</label>
                    <input type="text" class="form-control" id="username" placeholder="请输入用户名" autocomplete="off"
                    v-model.trim="username">
                </div>
                <div class="form-group">
                    <label for="password">密码：</label> <!-- 修改 label 的 for 属性 -->
                    <input type="password" class="form-control" id="password" placeholder="请输入密码"  v-model="password">
                </div>
                <div class="form-group">
                    <button type="button" class="btn" @click="onLogin">登录</button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
    import { ref } from "vue";
    import { useRouter } from 'vue-router';

    const router = useRouter(); // 确保在顶层调用

    const username = ref('');
    const password = ref('');

    const onLogin = () => {
        if (username.value === 'admin' && password.value === '123456') {
            router.push('/home'); // 使用 router 实例进行路由跳转
            localStorage.setItem('token', 'Bearer 123456');
        } else {
            alert('用户或密码输入错误~');
            localStorage.removeItem('token');
        }
    };
</script>
<style scoped>
.login-container {
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
  height: 100vh; /* 占满整个视口高度 */
  background-color: #f5f7fa; /* 可选背景色 */
}

.login-box {
  background-color: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  width: 400px;
}

.avatar-box {
  display: flex;
  justify-content: center;
  margin-bottom: 1rem;
}

.form-login .form-group {
  margin-bottom: 1rem;
}
.form-control {
  width: 100%; /* 统一宽度 */
  padding: 0.5rem;
  border: 1px solid #ccc;
  border-radius: 4px;
  outline: none;
  transition: border-color 0.3s;
}

.form-control:focus {
  border-color: #409EFF; /* 聚焦时边框颜色 */
}
.form-group {
  display: flex;
  align-items: center;
  margin-bottom: 1rem;
}

.form-group label {
  margin-right: 0.5rem;
  font-size: 1rem;
  color: #666;
}
.form-group:last-child {
  margin-top: 1rem; /* 按钮与输入框间距 */
}

.btn {
  font-size: 1rem;
  line-height: 1.5;
}
.btn {
  width: 100%;
  padding: 0.5rem;
  background-color: #409EFF;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.btn:hover {
  background-color: #337ecc;
}
</style>